package com.woniuxy.handler;

import com.woniuxy.service.NoticeService;
import com.work.framework.pojo.Notice;
import com.work.framework.pojo.ResultDTO;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: wanghao
 * @Date: 2019/9/18 10:24
 */
@Api(value = "处理通知方面的请求",tags = "竞拍通知")
@RestController
@RequestMapping("/notice")
public class NoticeHandler {

    @Resource
    private NoticeService noticeService;

    @ApiOperation(value = "给参与竞拍的人和收藏此商品的人发送商品即将开始")
    @PostMapping("/sendStart/{gid}")
    public Boolean sendStartMessage(@PathVariable("gid") Integer gid) {
        //数据验证
        if (gid == null || gid == 0) {
            return null;
        }
        //调用service业务处理
        Boolean isStartOk = noticeService.sendStartMessage(gid);
        return isStartOk;
    }

    @ApiOperation(value = "给参与竞拍的人和收藏此商品的人发送商品即将结束")
    @PostMapping("/sendEnd/{aid}")
    public Boolean sendEndMessage(@PathVariable("aid") Integer aid) {
        //数据验证
        if (aid == null || aid == 0) {
            return null;
        }
        //调用service业务处理
        Boolean isEndOk = noticeService.sendEndMessage(aid);
        return isEndOk;
    }

    @ApiOperation(value = "给抢拍成功的人发送抢拍成功的通知")
    @GetMapping("/sendSuccess/{uid}/{gid}")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "uid",value = "用户编号", dataType = "integer", paramType = "query", required = true),
//            @ApiImplicitParam(name = "gid",value = "商品编号", dataType = "integer", paramType = "query", required = true)
//    })
    @ApiResponses({
            @ApiResponse(code = 404, message = "服务未找到"),
            @ApiResponse(code = 500, message = "微服务网关发生错误")
    })
    public Boolean sendSuccessMessage(@PathVariable("uid") Integer uid,@PathVariable("gid") Integer gid) {
        //数据验证
        if (uid == null || gid==null) {
            return false;
        }
        //调用service业务处理
        Boolean isSuccessOk = noticeService.sendSuccessMessage(uid, gid);
        System.out.println("调用发送短信业务："+isSuccessOk);
        return isSuccessOk;
    }

    @ApiOperation(value = "存储买家和卖家发送的信息")
    @PostMapping("/addNotice")
    public Boolean addChatNotice(Notice notice) {
        //数据验证
        if (notice == null || notice.getRUid() == null || notice.getSUid()==null || notice.getMes() ==null) {
            return null;
        }
        Boolean isSendNoticeOk = noticeService.addChatNotice(notice);
        return isSendNoticeOk;
    }

    @ApiOperation(value = "查找买家和卖家发送的信息")
    @GetMapping("/findRecord/{sUid}/{rUid}/{mType}")
    public ResultDTO findChatNotice(@PathVariable("sUid") Integer sUid, @PathVariable("rUid") Integer rUid, @PathVariable("mType") Integer mType) {
        return noticeService.findNoticeByUid(sUid, rUid, mType);
    }

    @ApiOperation(value = "根据接收用户编号查找通知", notes = "返回该用户的所有通知")
    @GetMapping("/findNotice/{rUid}")
    public ResultDTO findNotice(@PathVariable("rUid") Integer rUid, HttpSession session) {
        //将
        //查询所有通知，将发送用户设置为null，消息类型设置为2（通知）
        ResultDTO noticeDto = noticeService.findNoticeByUid(null, rUid, 2);
        if (null != noticeDto.getData()) {
            //将通知集合放入到session中
            session.setAttribute("notices", noticeDto.getData());
            System.out.println(session.getId());
        }
        return noticeDto;
    }

    @ApiOperation(value = "从session中获取到当前登录用户的所有通知信息")
    @GetMapping("/getNotices")
    public
    ResultDTO getNotices (HttpSession session) {
        ResultDTO dto = new ResultDTO();
        List<Notice> notices = (List<Notice>)session.getAttribute("notices");
        if (null == notices || 0 == notices.size()) {
            dto.setCode(500);
            dto.setMessage("您还未登录！");
            dto.setTatol(0);
            return dto;
        }
        //状态码置为成功
        dto.setCode(200);
        //将数据放入
        dto.setData(notices);
        //统计数量
        dto.setTatol(notices.size());
        //提示成功
        dto.setMessage("查询通知信息成功！");
        return dto;
    }


}
